package com.rr.event;

import com.rr.entity.AddAction;
import lombok.*;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.ResolvableType;
import org.springframework.core.ResolvableTypeProvider;

@Getter
@Setter
public class BaseEvent<T> extends ApplicationEvent implements ResolvableTypeProvider {

    private final T data;
    public BaseEvent(T source) {
        super(source);
        this.data = source;
    }

    /**
     * 重写getResolvableType方法，返回当前类的泛型类型，这样消费者能直接根据泛型类型监听到消息
     * 见com.rr.event.listener.ActionListener
     * 不重写此方法，消费者只会监听到所有BaseEvent事件
     * 加上此方法，消费者会监听到具体不同泛型的方法：BaseEvent<AddAction>和BaseEvent<UpdateAction>
     * @return
     */
    @Override
    public ResolvableType getResolvableType() {
        return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(data));
    }

}
